package com.abe.libquick.widget.popup;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.abe.libcore.view.recyclerview.RecyclerViewTool;
import com.abe.libquick.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.List;

public class DropDownPopup<T extends DropPopupAbsEntity> extends PopupWindow {

    private View contentView;
    private Context context;
    private List<T> entities;
    private OnDropDownListener<T> listener;

    public DropDownPopup(Context context, List<T> entities) {
        super(context);
        this.context = context;
        this.entities = entities;
        contentView = contentView();
    }

    public void setListener(OnDropDownListener<T> listener) {
        this.listener = listener;
    }

    @SuppressLint("InflateParams")
    private View contentView() {
        View view = LayoutInflater.from(context).inflate(R.layout.item_view_drop_down_rv, null, false);
        RecyclerView itemRv = view.findViewById(R.id.item_rv);
        view.findViewById(R.id.item_empty_v).setOnClickListener(v -> dismiss());
        BaseQuickAdapter<T, BaseViewHolder> adapter1 = new BaseQuickAdapter<T, BaseViewHolder>(R.layout.item_view_drop_down, entities) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, T item) {
                helper.setText(R.id.item_name, item.name());
            }
        };
        adapter1.setOnItemClickListener((adapter, view1, position) -> {
            dismiss();
            if (listener == null) return;
            listener.onSelect(position, entities.get(position));
        });
        itemRv.setLayoutManager(new LinearLayoutManager(context));
        itemRv.addItemDecoration(RecyclerViewTool.colorRes(context, R.color.txt_d));
        itemRv.setAdapter(adapter1);
        return view;
    }

    @Override
    public void showAsDropDown(View view) {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wmManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wmManager.getDefaultDisplay().getMetrics(dm);
        setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.half_trans_black)));
        setWidth(dm.widthPixels);
        int[] location = new int[2];
        view.getLocationInWindow(location);
        view.getLocationOnScreen(location);
        setHeight(dm.heightPixels - location[1] - view.getHeight());
        setContentView(contentView);
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        super.showAsDropDown(view);
    }

    public void showAsDropDownInDialog(View view, int dialogHeight) {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wmManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wmManager.getDefaultDisplay().getMetrics(dm);
        setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.half_trans_black)));
        setWidth(view.getWidth());
        int[] location = new int[2];
        view.getLocationInWindow(location);
        view.getLocationOnScreen(location);
        int x = (dm.heightPixels - getStatusBarHeight() - dialogHeight) / 2;
        setHeight(dialogHeight + x + getStatusBarHeight() - location[1] - view.getHeight());
        setContentView(contentView);
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        super.showAsDropDown(view);
    }

    /**
     * 获取状态栏高度
     */
    public int getStatusBarHeight() {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelSize(resourceId);
    }

    public interface OnDropDownListener<T> {
        void onSelect(int pos, T entity);
    }
}
